package com.lyl.momanapp.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lyl.momanapp.R;
import com.lyl.momanapp.adapter.AddressesAdapter;
import com.lyl.momanapp.bean.URL;
import com.lyl.momanapp.bean.User;
import com.lyl.momanapp.util.utlis.MyUtils;
import com.lyl.momanapp.util.view.MyBaseActivity;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 名字 梁亚龙
 * 时间 2017/9/26.
 * QQ邮箱 503934353@qq.com
 * 用户的全部地址页面
 */

public class UserAddressesGuanliActivity extends MyBaseActivity {

    private ArrayList<User.listAddress.ResultBean> arrayList = new ArrayList<>();
    private ListView mLV;
    private AddressesAdapter mAddressesAdapter;
    private TextView mAdd;
    private TextView mSetAddress;
    private int listnum = 0;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //收货地址列表返回数据
                case URL.ID_LIST_ADDRESS: {
                    User.listAddress listAddress = new Gson().fromJson(msg.obj.toString(), User.listAddress.class);
                    if (listAddress.getState().equals("M00000")) {
                        arrayList = (ArrayList<User.listAddress.ResultBean>) listAddress.getResult();
                        mAddressesAdapter.adddata(arrayList);
                    } else {
                        Toast.makeText(UserAddressesGuanliActivity.this, listAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
                //删除收货地址
                case URL.ID_DELETE_ADDRESS: {
                    User.deleteAddress deleteAddress = new Gson().fromJson(msg.obj.toString(), User.deleteAddress.class);
                    if (deleteAddress.getState().equals("M00000")) {
                        HashMap<String, String> map = new HashMap<>();
                        map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
                        new MyUtils().xUtilsPost(URL.URL_LIST_ADDRESS, map, handler, URL.ID_LIST_ADDRESS);
                        Toast.makeText(UserAddressesGuanliActivity.this, deleteAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(UserAddressesGuanliActivity.this, deleteAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
                case URL.ID_SET_DEFAULT: {
                    User.setAddress setAddress = new Gson().fromJson(msg.obj.toString(), User.setAddress.class);
                    if (setAddress.getState().equals("M00000")){
                        HashMap<String, String> map = new HashMap<>();
                        map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
                        new MyUtils().xUtilsPost(URL.URL_LIST_ADDRESS, map, handler, URL.ID_LIST_ADDRESS);
                        Toast.makeText(UserAddressesGuanliActivity.this, setAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(UserAddressesGuanliActivity.this, setAddress.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                break;
                case R.id.fanhui:{
                    finish();
                }
                break;
                case R.id.zhuye: {

                    startActivity(new Intent(UserAddressesGuanliActivity.this, MainActivity.class));
                    finish();
                }
            }
        }
    };


    @Override
    public void initUI() {
        setContentView(R.layout.activity_user_addresses_guanli);
        mLV = ((ListView) findViewById(R.id.address_guanli_lv));
        mAdd = ((TextView) findViewById(R.id.addresses_add));
        mSetAddress = ((TextView) findViewById(R.id.addresses_set));
        findViewById(R.id.fanhui).setOnClickListener(this);
        findViewById(R.id.zhuye).setOnClickListener(this);
    }

    @Override
    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
        new MyUtils().xUtilsPost(URL.URL_LIST_ADDRESS, map, handler, URL.ID_LIST_ADDRESS);
        mAddressesAdapter = new AddressesAdapter(UserAddressesGuanliActivity.this, arrayList);
        mLV.setAdapter(mAddressesAdapter);
        mAddressesAdapter.setOnItemClickLitener(new AddressesAdapter.OnItemClickLitener() {
            @Override
            public void onCheckBoxClick(View view, int position) {
                listnum = position;
            }

            @Override
            public void onItemClick(View view, int position) {
               URL.address_id= arrayList.get(position).getAddress_id();
                finish();

            }

            @Override
            public void ondelClick(View view, int position) {
                HashMap<String, String> map = new HashMap<>();
                map.put("user_id", URL.Userlogin.getResult().getUser_id() + "");
                map.put("address_id", arrayList.get(position).getAddress_id() + "");
                new MyUtils().xUtilsPost(URL.URL_DELETE_ADDRESS, map, handler, URL.ID_DELETE_ADDRESS);
            }

            @Override
            public void onxiugaiClick(View view, int position) {
                Intent intent = new Intent(UserAddressesGuanliActivity.this, UserAddressActivity.class);
                User.listAddress.ResultBean resultBean = arrayList.get(position);
                intent.putExtra("resultBean",resultBean);
                startActivity(intent);
            }
        });
    }

    @Override
    public void addListeners() {
        mAdd.setOnClickListener(this);
        mSetAddress.setOnClickListener(this);
        findViewById(R.id.fanhui).setOnClickListener(this);
        findViewById(R.id.zhuye).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addresses_add:
                startActivity(new Intent(UserAddressesGuanliActivity.this, UserAddressActivity.class));
                break;
            case R.id.addresses_set: {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("user_id",URL.Userlogin.getResult().getUser_id()  + "");
                map.put("address_id",arrayList.get(listnum).getAddress_id()+"");
                new MyUtils().xUtilsPost(URL.URL_SET_DEFAULT, map, handler, URL.ID_SET_DEFAULT);
            }
            break;
            case R.id.fanhui: {
                    finish();
            }
            break;
            case R.id.zhuye: {
                startActivity(new Intent(UserAddressesGuanliActivity.this, MainActivity.class));
            }
            break;

        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id", URL.Userlogin.getResult().getUser_id()  + "");
        new MyUtils().xUtilsPost(URL.URL_LIST_ADDRESS, map, handler, URL.ID_LIST_ADDRESS);
        Log.i("1", "onRestart: "+"刷新数据");

    }
}
